<template>
  <el-container class="main">
    <el-header>
      <img src="../assets/images/logo.png" alt="" />
      <el-menu mode="horizontal" background-color="#23262e" text-color="#fff" active-text-color="#409eff">
        <el-submenu index="1">
          <template slot="title"
            ><img class="avatar" :src="user.user_pic" alt="" v-if="user.user_pic" />
            <img class="avatar" src="../assets/logo.png" alt="" v-else /> 个人中心</template
          >
          <el-menu-item index="1-1"><i class="el-icon-s-operation"></i>基本资料</el-menu-item>
          <el-menu-item index="1-2"><i class="el-icon-camera"></i>更换头像</el-menu-item>
          <el-menu-item index="2-3"><i class="el-icon-key"></i>重置密码</el-menu-item>
        </el-submenu>
        <el-menu-item index="2" @click="open"><i class="el-icon-switch-button"></i>退出</el-menu-item>
      </el-menu>
    </el-header>
    <el-container>
      <el-aside width="200px">
        <div class="user-info">
          <img class="avatar" :src="user.user_pic" alt="" v-if="user.user_pic" />
          <img class="avatar" src="../assets/logo.png" alt="" v-else />
          欢迎 {{ user.nickname || user.username }}
        </div>
        <el-menu
          background-color="#23262e"
          text-color="#fff"
          active-text-color="#409eff"
          unique-opened
          class="aside-menu"
          :default-active="$route.path"
          router
        >
          <template v-for="item in menus">
            <el-menu-item :index="item.indexPath" v-if="item.children === null" :key="item.indexPath">
              <i :class="item.icon"></i>{{ item.title }}</el-menu-item
            >
            <el-submenu :index="item.indexPath" v-else :key="item.indexPath">
              <template slot="title"><i :class="item.icon"></i>{{ item.title }}</template>
              <el-menu-item :index="subItem.indexPath" v-for="subItem in item.children" :key="subItem.indexPath"
                ><i :class="subItem.icon"></i>{{ subItem.title }}</el-menu-item
              >
              <!-- <el-menu-item :index="subItem.indexPath"><i :class="subItem.icon"></i>{{subItem.title}}</el-menu-item> -->
            </el-submenu>
          </template>
        </el-menu>
      </el-aside>
      <el-container>
        <el-main><router-view></router-view></el-main>
        <el-footer>© www.itheima.com - 黑马程序员</el-footer>
      </el-container>
    </el-container>
  </el-container>
</template>

<script>
import { mapState } from 'vuex'
import { getMenusAPI } from '@/APl/menu.js'
export default {
  data() {
    return {
      menus: []
    }
  },
  async created() {
    this.$store.dispatch('user/getUser')
    const { data: res } = await getMenusAPI()
    if (res.code === 0) {
      this.menus = res.data
    }
  },
  computed: {
    ...mapState('user', ['user'])
  },
  methods: {
    open() {
      this.$confirm('此操作将退出登录, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          this.$store.commit('user/updateToken', '')
          this.$router.push('/login')
        })
        .catch(() => {
          this.$router.push('/')
        })
    }
  }
}
</script>
<style lang="less" scoped>
.main {
  height: 100%;
  .avatar {
    width: 35px;
    height: 35px;
    background-color: #fff;
    border-radius: 50%;
    margin-right: 15px;
    object-fit: cover;
  }
  .user-info {
    color: #fff;
    height: 70px;
    display: flex;
    justify-content: center;
    align-items: center;
    font-size: 12px;
  }
  .el-header,
  .el-aside {
    background-color: #23262e;
  }
  .el-main {
    background-color: #f2f2f2;
  }
  .el-footer {
    background-color: #eee;
    font-size: 12px;
    display: flex;
    justify-content: center;
    align-items: center;
  }
  .el-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
  }
  .aside-menu {
    width: 200px;
  }
  .avatar {
    width: 35px;
    height: 35px;
    border-radius: 50%;
    margin-right: 15px;
    object-fit: cover;
  }
}
</style>
